<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require_once('base.php');

class User extends Base {
	
	public function getPublicInfo($id)
	{
		$this->load->model('User_model');
		
		echo json_encode(array('result'=>$this->User_model->getPublicInfo($id)));
	}
	
	public function follow()
	{
		$user = $this->getUser();
		if ($user == null)	{
			echo json_encode(array('result'=>'error', 'message'=>'Access denied'));
			return;
		}
		
		$id = $this->input->post('id');
		if ($id == null)	{
			echo json_encode(array('result'=>'error', 'message'=>'Invalid id'));
			return;
		}
		
		$this->load->model('Follow_model');
		$this->Follow_model->followUser($user->id, $id);
		echo json_encode(array('result'=>'success'));
	}
	
	public function editPublicInfo($id)
	{
		$user = $this->getUser();
		if ($user == null)	{
			echo json_encode(array('result'=>'error', 'message'=>'Access denied'));
			return;
		}
		$user->name = $this->input->post('name');
		$user->address = $this->input->post('address');
		$user->gender = $this->input->post('gender');
		$user->dob = $this->input->post('dob');
		
		echo json_encode(array('result'=>$user->editPublicInfo($user->id)));
	}
	
	public function register()
	{
		$this->load->model('User_model');

		$this->User_model->email = $this->input->post('email', true);
		$this->User_model->password = $this->input->post('password', true);
		$this->User_model->name = $this->input->post('name', true);
		
		echo json_encode(array('result'=>$this->User_model->register()));
	}
	
	public function login()	
	{
		$this->load->model('User_model');
		$this->User_model->email = $this->input->post('email', true);
		$this->User_model->password = $this->input->post('password', true);
		
		$result = $this->User_model->login();
		if ($result === null) {
			$result = false;	
		}
		else {
			$this->load->library('session');
			$this->session->set_userdata('user', $result);
			$result = true;
		}
		echo json_encode(array('result'=>$result));
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */